/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package yuhuibear.poat.object.bean;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import yuhuibear.poat.annotations.relation.Table;
import yuhuibear.poat.test.entity.PersonalProfile;
import yuhuibear.poat.test.entity.User;
import yuhuibear.poat.util.MyAssert;
import static org.junit.Assert.*;

/**
 * 
 * @author yuhuibear
 */
public class BeanStructTest {

	private static PersistentClassLoader bas;

	public BeanStructTest() {}

	@BeforeClass
	public static void setUpClass() throws Exception {
		bas = PersistentClassLoader.getInstance();
	}

	@AfterClass
	public static void tearDownClass() throws Exception {}

	@Before
	public void setUp() {}

	@After
	public void tearDown() {}

	/**
	 * Test of hasAnnotation method, of class BeanStruct.
	 */
	@Test
	public void testHasAnnotation() {

		System.out.println("hasAnnotation");
		try {
			Class type = Table.class;
			BeanStruct instance = bas.analyse(User.class);
			boolean expResult = true;
			boolean result = instance.hasAnnotation(type);
			assertEquals(expResult, result);

			expResult = false;
			result = instance.hasAnnotation(null);
			assertEquals(expResult, result);
		} catch (Exception ex) {
			Logger.getLogger(BeanStructTest.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	/**
	 * Test of getAnnotation method, of class BeanStruct.
	 */
	@Test
	public void testGetAnnotation() {
		System.out.println("getAnnotation");
		try {
			Class type = Table.class;
			BeanStruct instance = bas.analyse(User.class);
			// Class expResult = type;
			Table obj = (Table) instance.getAnnotation(type);
			assertEquals("users", obj.name());

			assertNull(instance.getAnnotation(null));
		} catch (Exception ex) {
			Logger.getLogger(BeanStructTest.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	/**
	 * Test of getDBNameOfAttributes method, of class BeanStruct.
	 */
	@Test
	public void testGetDBNameOfAttributes() {
		try {
			System.out.println("getDBNameOfAttributes");
			BeanStruct instance = bas.analyse(User.class);
			String[] expResult = new String[] {
					"credite",
					"commentby",
					"id",
					"name" };
			String[] result = instance.getDBNameOfAttributes();
			MyAssert.arrayEqualsNoOrder(expResult, result);
		} catch (Exception ex) {
			Logger.getLogger(BeanStructTest.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	/**
	 * Test of getFullDBNameOfAttributes method, of class BeanStruct.
	 */
	@Test
	public void testGetFullDBNameOfAttributes_0args() {
		try {
			System.out.println("getFullDBNameOfAttributes");
			BeanStruct instance = bas.analyse(User.class);
			String[] expResult = new String[] {
					"users.id",
					"users.name",
					"users.credite",
					"users.commentby" };
			String[] result = instance.getFullDBNameOfAttributes();
			MyAssert.arrayEqualsNoOrder(expResult, result);
		} catch (Exception ex) {
			Logger.getLogger(BeanStructTest.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	/**
	 * Test of getFullDBNameOfAttributes method, of class BeanStruct.
	 */
	@Test
	public void testGetFullDBNameOfAttributes_Object() throws Exception {
		System.out.println("getFullDBNameOfAttributes");
		Object obj = new User();

		BeanStruct instance = bas.analyse(User.class);
		String[] expResult = new String[] {
				"users.name",
				"users.credite",
				"users.commentby" };

		String[] result = instance.getFullDBNameOfAttributes(obj);
		MyAssert.arrayEqualsNoOrder(expResult, result);
	}

	/**
	 * Test of geDBtNameOfAttributes method, of class BeanStruct.
	 */
	@Test
	public void testGeDBtNameOfAttributes() throws Exception {
		System.out.println("geDBtNameOfAttributes");
		Object obj = new User();

		BeanStruct instance = bas.analyse(User.class);
		String[] expResult = new String[] { "name", "credite", "commentby" };

		String[] result = instance.getDBNameOfAttributes(obj);
		MyAssert.arrayEqualsNoOrder(expResult, result);
	}

	/**
	 * Test of getFullNameOfAttributes method, of class BeanStruct.
	 */
	@Test
	public void testGetFullNameOfAttributes() {
		try {
			System.out.println("getFullNameOfAttributes");
			BeanStruct instance = bas.analyse(User.class);
			String[] expResult = new String[] {
					"User.id",
					"User.name",
					"User.credite",
					"User.cmt" };
			String[] result = instance.getFullNameOfAttributes();
			MyAssert.arrayEqualsNoOrder(expResult, result);
		} catch (Exception ex) {
			Logger.getLogger(BeanStructTest.class.getName()).log(Level.SEVERE, null, ex);
		}

	}

	/**
	 * Test of getNameOfAttributes method, of class BeanStruct.
	 */
	@Test
	public void testGetNameOfAttributes() {
		try {
			System.out.println("getNameOfAttributes");
			BeanStruct instance = bas.analyse(User.class);
			String[] expResult = new String[] { "id", "name", "cmt", "credite" };
			String[] result = instance.getNameOfAttributes();
			MyAssert.arrayEqualsNoOrder(expResult, result);
		} catch (Exception ex) {
			Logger.getLogger(BeanStructTest.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	/**
	 * Test of getAttributeMappedNameByFullName method, of class BeanStruct.
	 */
	@Test
	public void testGetAttributeMappedNameByFullName() {
		try {
			System.out.println("getAttributeMappedNameByFullName");
			String name = "User.cmt";
			BeanStruct instance = bas.analyse(User.class);
			String expResult = "commentby";
			String result = instance.getAttributeMappedNameByName(name);
			assertEquals(expResult, result);

			instance = bas.analyse(PersonalProfile.class);
			name = "PersonalProfile.commentContent";
			expResult = "comment_content";
			result = instance.getAttributeMappedNameByName(name);
			assertEquals(expResult, result);
		} catch (Exception ex) {
			Logger.getLogger(BeanStructTest.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	/**
	 * Test of getAttributeMappedFullNameByName method, of class BeanStruct.
	 */
	@Test
	public void testGetAttributeMappedFullNameByName() {
		try {
			System.out.println("getAttributeMappedFullNameByName");
			String name = "User.cmt";
			BeanStruct instance = bas.analyse(User.class);
			String expResult = "users.commentby";
			String result = instance.getAttributeMappedFullNameByName(name);
			assertEquals(expResult, result);

			instance = bas.analyse(PersonalProfile.class);
			name = "PersonalProfile.commentContent";
			expResult = "personal_profiles.comment_content";
			result = instance.getAttributeMappedFullNameByName(name);
			assertEquals(expResult, result);
		} catch (Exception ex) {
			Logger.getLogger(BeanStructTest.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

}
